/*
 * Copyright (c) 2000-2017 wanglei. All rights reserved.
 * TeamDev PROPRIETARY and CONFIDENTIAL.
 * Use is subject to license terms.
 */

package com.wlit.ui;

import org.cef.CefApp;
import org.cef.CefClient;
import org.cef.browser.CefBrowser;
import org.cef.browser.CefFrame;
import org.cef.browser.CefMessageRouter;
import org.cef.handler.CefDisplayHandlerAdapter;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * @author wanglei.
 */
public class TabContent extends JPanel {

    private final ToolBar toolBar;
    private final JComponent container;
    private final JComponent browserContainer;

    private final CefClient client;

    private final CefBrowser browser;

    public TabContent(final CefApp cefApp,String url) {
        client = cefApp.createClient();
        browser = client.createBrowser(url, false, false);
        CefMessageRouter msgRouter = CefMessageRouter.create();
        client.addMessageRouter(msgRouter);
        browserContainer = createBrowserContainer();
        toolBar = createToolBar();
        client.addDisplayHandler(new CefDisplayHandlerAdapter() {
            @Override
            public void onAddressChange(CefBrowser browser, CefFrame frame, String url) {
                toolBar.setUrl(url);
            }

            @Override
            public void onTitleChange(CefBrowser browser, String title) {
                firePropertyChange("PageTitleChanged", null, title);
            }
        });

        container = new JPanel(new BorderLayout());
        container.add(browserContainer, BorderLayout.CENTER);

        setLayout(new BorderLayout());
        add(toolBar, BorderLayout.NORTH);
        add(container, BorderLayout.CENTER);
    }

    private ToolBar createToolBar() {
        ToolBar toolBar = new ToolBar(client,browser);
        toolBar.addPropertyChangeListener("TabClosed", new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                firePropertyChange("TabClosed", false, true);
            }
        });

        toolBar.addPropertyChangeListener("JSConsoleClosed", new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                hideConsole();
            }
        });
        return toolBar;
    }

    public ToolBar getToolBar(){
        return toolBar;
    }

    private void hideConsole() {
        showComponent(browserContainer);
    }

    private void showComponent(JComponent component) {
        container.removeAll();
        container.add(component, BorderLayout.CENTER);
        validate();
    }

    private void showConsole() {
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        splitPane.add(browserContainer, JSplitPane.TOP);
        splitPane.setResizeWeight(0.8);
        splitPane.setBorder(BorderFactory.createEmptyBorder());
        showComponent(splitPane);
    }



    private JComponent createBrowserContainer() {
        JPanel container = new JPanel(new BorderLayout());
        browser.getUIComponent().setFocusable(false);
        container.add(browser.getUIComponent(), BorderLayout.CENTER);
        return container;
    }

    public void dispose() {
        browser.doClose();
        client.dispose();
    }
}
